package com.meetup.caug.gitrdone;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.meetup.caug.gitrdone.model.Task;

public class TaskList extends ListActivity {

    MainApplication ma;

    List<Task> taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_list);

        ma = (MainApplication) getApplication();
        taskList = ma.getTasks();

        // Verify our list size and make it easy to find
        Log.v("ez", "taskList size " + taskList.size());

        TaskListAdapter adapter = new TaskListAdapter(this);
        setListAdapter(adapter);
    }

    public class TaskListAdapter extends BaseAdapter {

        public TaskListAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return taskList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                LayoutInflater vi = LayoutInflater.from(this.mContext);
                convertView = vi.inflate(R.layout.task_item, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Task task = taskList.get(position);
            if (task != null) {
                holder.toptext = (TextView) convertView
                        .findViewById(R.id.taskName);
                holder.toptext.setText(task.getName());
            }
            return convertView;
        }

        private Context mContext;
    }

    public static class ViewHolder {
        TextView toptext;
    }
}
